package myProj2;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import myProj2.Code;
import myProj2.Project2.Color;

public class Project2 {
	public static enum Color {EMPTY, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET}
	public static int SEED = 321;
	public static Random rangen = new Random(SEED);
	public static Scanner scnr = new Scanner(System.in);
	public static Scanner entryScnr;
	private static final int TRIALS = 1;

	public static ArrayList<Code> allOpts(){
		ArrayList<Code> result = new ArrayList<Code>();
		for(int i=0;i<7;i++){
			for(int j=0;j<7;j++){
				for(int k=0;k<7;k++){
					for(int m=0;m<7;m++){
						result.add(new Code(new Color[] {Color.values()[i], 
								Color.values()[j], Color.values()[k], Color.values()[m]}));
					}
				}
			}
		}
		return result;
	}

	public static void main(String[] args){
		int turns, pegAns, totTurns = 0, numGames=0;
		String makerName, breakerName, entry;
		boolean makerAI, breakerAI, hasHumans, gameOver;
		ArrayList<Code> remaining=new ArrayList<Code>();
		char ans='Y';
		Code solution, myGuess =new Code(); //just filler for initialization
		do{
			System.out.print("Enter name for the codeMaker: ");
			makerName = scnr.nextLine();
		}while (makerName.length()==0);
		makerAI = makerName.length()>1 && //uses short-circuit
				makerName.substring(0, 2).toUpperCase().equals("AI");
		do{
			System.out.print("Enter a different name for the codeBreaker: ");
			breakerName = scnr.nextLine();
		}while (breakerName.length()==0 || makerName.equals(breakerName));
		breakerAI = breakerName.length()>1 && 
				breakerName.substring(0, 2).toUpperCase().equals("AI");
		//gameplay starts here
		hasHumans = !makerAI || !breakerAI|| numGames>=TRIALS;;
		while(!hasHumans&&numGames<TRIALS || hasHumans&&ans =='Y'){
			remaining = allOpts();
			turns=0;
			pegAns=0;
			gameOver = false;
			System.out.println("\nPlaying game #"+ ++numGames);
			hasHumans = !makerAI || !breakerAI || numGames>=TRIALS;
			solution = Provided.randColors();
			if(makerAI){
				if(hasHumans){
				System.out.println(makerName+" has made a code for "+breakerName+" to break.");
				}
			}else{
				System.out.println(makerName+" should mentally make a code for "
							+breakerName+" to break.");
			}
			do{
				turns++;
				if(hasHumans)System.out.println("\nThis is game#"+numGames+", turn #"+ turns);
				if(breakerAI){
					if(turns==1){
						//myGuess = Provided.allSame();
						myGuess = Provided.twoDubs();//TODO: can change this as needed
					}else{//note that myGuess and pegAns exist from previous turn
						myGuess = myGuess.pickMove(remaining, pegAns); 
						if(myGuess==null){ //only happens if human maker in error/lies
							System.out.println(breakerName+" cannot make guess. "+
										"Inconsistent peg input. Terminating game");
							gameOver=true;
						}
					}
					if(hasHumans && !(myGuess==null)){
						System.out.println(breakerName+" entered "+myGuess);
					}
				}else{ //random from remaining arraylist, unless turn1
					myGuess = Provided.inputColors();
				}
				if(!gameOver){
					if(makerAI){
						pegAns = myGuess.pegs(solution);
					}else{
						pegAns = Provided.inputPegs();
					}
					if(hasHumans)System.out.println(breakerName+" guessed "+myGuess+", and "+
							makerName+" declares: Black = "+pegAns/10+", White = "+pegAns%10);
					if(pegAns==40){
						gameOver = true;
						if(hasHumans) System.out.println(breakerName+" broke the code of "
								+makerName+" on turn#"+turns);
					}else if(turns==10){
						gameOver = true;
						if(hasHumans){
							System.out.println(breakerName+" was unable to break the "+"code of "+makerName+
									" within the ten turn limit");
							if(makerAI){
								System.out.println("The solution was "+solution);
							} else{
								System.out.println("The solution will be verbally revealed by "+makerName+" now");
							}
						}
					}
				}
			} while(!gameOver);
			if(hasHumans){
				do{
					System.out.print("Do you wish to play again (Y/N)? ");
					entry = scnr.nextLine();
				}while(entry.length()==0);
				ans = entry.toUpperCase().charAt(0);
			}
			totTurns+=turns;
			turns=0;
		}
		System.out.println("\nThe average game's turn length was "+(double)totTurns/numGames);
	}
}
